<style lang="less">
  .card_style {
    height: 100%;
    width: 100%;
    overflow: hidden;
    .ivu-card-body {
      padding: 0px;
    }
  }
  .page_style {
    margin-top: 10px;
    text-align: right;
  }
  .bench_search {
    margin-bottom: 10px;
  }
  .nopadding .ivu-table-cell {
    padding: 0px 0px;
  }
  .item1{
    height: 42px;
    line-height: 40px;
  }
  .new-btn-sty {
    margin-right: 5px;
    color: #fff;
    background-color: #235CA7;
    border-color: #235CA7;
    font-weight: 500;
  }
</style>

<template>
  <Card class="card_style" :bordered="false" :dis-hover="true">
    <Row class="bench_search">
      <Col span="6" offset="1">
        <span>标题：</span>
        <Input style="width: 70%;" v-model="searchInfo.title" />
      </Col>
      <Col span="5" v-if="companyCode === 'LM'">
        <span>状态：</span>
        <Select v-model="searchInfo.state" style="width: 75%;">
          <Option value="">全部</Option>
          <Option value="0">未发布</Option>
          <Option value="1">发布中</Option>
        </Select>
      </Col>
      <Button class="new-btn-sty" @click="searchData" style="margin-right:10px"><Icon type="ios-search" /> 搜索</Button>
      <Button class="new-btn-sty" @click="editNotice({})" v-if="companyCode === 'LM'"><Icon type="md-add" /> 创建</Button>
      <Modal v-model="modalEdit" title="通知基本信息" width="700" cancel-text="关闭"
        @on-ok="ok111(paramData2)" @on-cancel="modalEdit=false" ok-text="确定">
        <Form :model="paramData2" >
          <Row class="item1">
            <Col span="5" style="text-align: right" class="">标题:</Col>
            <Col span="12" offset="1">
              <Input v-model="paramData2.title"></Input>
            </Col>
          </Row>
          <Row class="item1" style="height:auto;line-height:auto">
            <Col span="5" style="text-align: right" class="">内容:</Col>
            <Col span="12" offset="1">
              <Input v-model="paramData2.content" type="textarea" :autosize="{minRows: 2,maxRows: 5}"></Input>
            </Col>
          </Row>
          <Row class="item1">
            <Col span="5" style="text-align: right" class="">排序:</Col>
            <Col span="12" offset="1">
              <Input v-model="paramData2.sort"></Input>
            </Col>
          </Row>
        </Form>
      </Modal>
      <Modal v-model="showNotice" title="通知详情" width="700">
        <Form :model="paramData">
          <Row class="item1">
            <Col span="6" style="text-align: right" class="red-name">标题:</Col>
            <Col span="12" offset="1">{{paramData.title}}</Col>
          </Row>
          <Row class="item1" style="height:auto;line-height:auto">
            <Col span="6" style="text-align: right" class="">内容:</Col>
            <Col span="12" offset="1">{{paramData.content}}</Col>
          </Row>
          <Row class="item1">
            <Col span="6" style="text-align: right" class="">排序:</Col>
            <Col span="12" offset="1">{{paramData.sort}}</Col>
          </Row>
          <Row class="item1">
            <Col span="6" style="text-align: right" class="red-name">发布时间:</Col>
            <Col span="12" offset="1">{{paramData.release_time}}</Col>
          </Row>
        </Form>
        <div slot="footer">
          <Button type="error" @click="showNotice=false">关闭</Button>
        </div>
      </Modal>
    </Row>
    <Table border :columns="columns1" :data="datalist"></Table>
    <!-- <Page :total="totalnum" :current="currentPageNo" :page-size="currentPageSize" @on-change="change" class="page_style"/> -->
  </Card>
</template>

<script>
import { noticeList, saveNotice, setReleaseState, deleteNotice } from '@/api/eventlist'
import { releaseNoticeList } from '@/api/mainv3'

export default {
  name: 'message',
  data () {
    return {
      showNotice: false,
      modalEdit: false,
      params: { pageNo: '', pageSize: '' },
      searchInfo: { title: '', state: '' },
      paramData: {},
      // 修改或添加新通知公告时使用
      paramData2: {},
      totalnum: 0,
      columns1: [
        { title: '序号', type: 'index', width: 70, align: 'center' },
        { title: '标题', key: 'title', width: 300, align: 'center' },
        {
          title: '状态',
          key: 'state',
          width: 250,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.state === '1' ? '发布中' : '未发布')
          }
        },
        { title: '创建时间', key: 'create_time', width: 250, align: 'center' },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          className: 'nopadding',
          minWidth: 60,
          render: (h, params) => {
            let temp = []
            if (this.companyCode === 'LM') {
              temp.push(
                h('Button', {
                  props: { type: 'primary', size: 'small' },
                  style: { marginRight: '5px', backgroundColor: '#598ED1', borderColor: '#598ED1' },
                  on: {
                    click: () => {
                      this.editNotice(params.row)
                    }
                  }
                }, '编辑'),
                h('Button', {
                  props: { type: 'info', size: 'small' },
                  style: { marginRight: '5px', backgroundColor: '#235CA7', borderColor: '#235CA7' },
                  on: {
                    click: () => {
                      this.showInfo(params.row)
                    }
                  }
                }, '详情'),
                h('Button', {
                  props: { type: 'error', size: 'small' },
                  style: { marginRight: '5px' },
                  on: {
                    click: () => {
                      this.remove(params.row)
                    }
                  }
                }, '删除')
              )
              if (params.row.state === '0') {
                temp.push(
                  h('Button', {
                    props: { type: 'success', size: 'small' },
                    on: {
                      click: () => {
                        this.setNoticeState(params.row, '1')
                      }
                    }
                  }, '发布')
                )
              }
              if (params.row.state === '1') {
                temp.push([
                  h('Button', {
                    props: { type: 'warning', size: 'small' },
                    on: {
                      click: () => {
                        this.setNoticeState(params.row, '0')
                      }
                    }
                  }, '撤销')
                ])
              }
            } else {
              temp.push(
                h('Button', {
                  props: { type: 'info', size: 'small' },
                  style: { marginRight: '5px', backgroundColor: '#235CA7', borderColor: '#235CA7' },
                  on: {
                    click: () => {
                      this.showInfo(params.row)
                    }
                  }
                }, '详情')
              )
            }
            return h('div', temp)
          }
        }
      ],
      datalist: []
    }
  },
  computed: {
    companyCode () {
      return this.$store.state.user.severUserInfo.company_code
    }
  },
  methods: {
    // 查询订单列表接口
    init (param) {
      if (this.companyCode === 'LM') {
        noticeList(param).then(res => {
          if (res.data.errcode === 0) {
            this.datalist = res.data.data
          } else {
            this.$Notice.error({ title: '获取通知公告列表失败', desc: res.data.errmsg })
          }
        })
      } else {
        this.columns1[3].key = 'release_time'
        this.columns1[3].title = '发布时间'
        releaseNoticeList(param).then(res => {
          if (res.data.errcode === 0) {
            this.datalist = res.data.data
          } else {
            this.$Notice.error({ title: '获取通知公告列表失败', desc: res.data.errmsg })
          }
        })
      }
    },
    ok111 (param) {
      saveNotice(param).then(res => {
        if (res.data.errcode === 0) {
          this.init(this.params)
          this.$Notice.success({ title: '成功', desc: '' })
        } else {
          this.$Notice.error({ title: '失败', desc: res.data.errmsg })
        }
      })
    },
    showInfo (item) {
      this.showNotice = true
      this.paramData.title = item.title
      this.paramData.content = item.content
      this.paramData.sort = item.sort
      this.paramData.release_time = item.release_time
    },
    editNotice (item) {
      this.modalEdit = true
      this.paramData2.id = item.id || null
      this.paramData2.title = item.title || ''
      this.paramData2.content = item.content || ''
      this.paramData2.sort = item.sort || ''
    },
    remove (item) {
      if (confirm('确定删除该条数据吗')) {
        deleteNotice({ id: item.id }).then(res => {
          if (res.data.errcode === 0) {
            this.init(this.params)
            return
          }
          this.$Notice.error({ title: '删除失败', desc: res.errmsg })
        })
      }
    },
    searchData () {
      let params = {
        state: this.searchInfo.state,
        title: this.searchInfo.title
      }
      this.currentPageNo = 1
      this.init(params)
    },
    change (pageNo) {
      this.currentPageNo = pageNo
      let params = {
        state: this.searchInfo.state,
        title: this.searchInfo.title
      }
      this.init(params)
    },
    // 发布or撤销,state==='1'时表示发布通知，state==='0'时表示撤销通知
    setNoticeState (item, state) {
      let msg = state === '1' ? '确定发布此通知吗！' : '确认撤销此通知吗！'
      var param = { id: item.id, state: state }
      if (confirm(msg)) {
        setReleaseState(param).then(res => {
          if (res.data.errcode === 0) {
            this.init(this.params)
            return
          }
          this.$Notice.error({ title: '失败', desc: res.errmsg })
        })
      }
    }
  },
  mounted () {
    this.init(this.params)
  }
}
</script>
